<body>
<script src="../../../resources/testharness.js"></script>
<script>
testRunner.dumpAsText();
function runScript(name, callback) {
  var script = document.createElement("script");
  script.src = name;
  script.onload = callback;
  document.head.appendChild(script);
}
var increment = 0;
var SameOriginTextScript = 1066,
    SameOriginApplicationScript = 1067,
    SameOriginOtherScript = 1068,
    CrossOriginTextScript = 1069,
    CrossOriginApplicationScript = 1070,
    CrossOriginOtherScript = 1071;
var counters = [SameOriginTextScript,
                SameOriginApplicationScript,
                SameOriginOtherScript,
                CrossOriginTextScript,
                CrossOriginApplicationScript,
                CrossOriginOtherScript];
function getUseCounters() {
  return counters.map(counter => internals.isUseCounted(document, counter));
}
function testCase(test, callback) {
  var previousCounters = getUseCounters();
  var previousIncrement = increment;
  var url = (test.crossOrigin ? "http://localhost:8000" : "") +
            "/mime/resources/javascript-mimetype.php?mimetype=" + test.mimetype;
  runScript(url, function() {
    assert_false(test.expectFailure === true);
    assert_equals(previousIncrement + 1, increment, "should have run the script");
    var newCounters = getUseCounters();
    for (var counter in counters) {
      if (counters[counter] === test.useCounter) {
        assert_equals(previousCounters[counter], !!test.secondTime);
        assert_equals(newCounters[counter], true);
      } else {
        assert_equals(previousCounters[counter], newCounters[counter]);
      }
    }
    alert("Correct");
    callback();
  });
  if (test.expectFailure) callback();
}
function nextCase(cases, i) {
  if (i >= cases.length)
    return function() {};
  else return function() {
    testCase(cases[i], nextCase(cases, i + 1));
  }
}
function runTestCases(cases) {
  nextCase(cases, 0)();
}

var initialCounters = getUseCounters();
for (var initialCounter of initialCounters) {
  assert_false(initialCounter);
}
runTestCases([
  // JS mimetypes don't increment any usecounters
  { mimetype: 'text/livescript', crossOrigin: false, useCounter: -1 },
  { mimetype: 'text/jscript', crossOrigin: true, useCounter: -1 },
  { mimetype: 'application/ecmascript', crossOrigin: false, useCounter: -1 },
  { mimetype: 'text/ecmascript', crossOrigin: true, useCounter: -1 },

  // Counters are not incremented on invalid use of images
  { mimetype: 'image/foo', crossOrigin: false, useCounter: -1, expectFailure: true },
  { mimetype: 'image/bar', crossOrigin: true, useCounter: -1, expectFailure: true },

  // Text mimetypes are registered separately
  { mimetype: 'text/html', crossOrigin: false, useCounter: SameOriginTextScript },
  { mimetype: 'text/csv', crossOrigin: false, useCounter: SameOriginTextScript, secondTime: true },

  { mimetype: 'text/html', crossOrigin: true, useCounter: CrossOriginTextScript },
  { mimetype: 'text/csv', crossOrigin: true, useCounter: CrossOriginTextScript , secondTime: true },

  { mimetype: 'foo/html', crossOrigin: false, useCounter: SameOriginOtherScript },
  { mimetype: 'foo/csv', crossOrigin: false, useCounter: SameOriginOtherScript, secondTime: true },

  { mimetype: 'foo/html', crossOrigin: true, useCounter: CrossOriginOtherScript },
  { mimetype: 'foo/csv', crossOrigin: true, useCounter: CrossOriginOtherScript, secondTime: true },

  { mimetype: 'application/csv', crossOrigin: false, useCounter: SameOriginApplicationScript },

  { mimetype: 'application/csv', crossOrigin: true, useCounter: CrossOriginApplicationScript },
]);
</script>
</body>
